using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using jQueryApi;

namespace Nsb.Classes
{
    public partial class Truple
    {
        private static int _tabIndex;

        [AlternateSignature]
        private extern Truple BuildTruple(string type, string fieldName);

        private Truple BuildTruple(string type, string fieldName, Truple appendTo)
        {
            string key = Strings.ParaSquishCase(fieldName);
            appendTo = Script.Value(appendTo, (Truple)(object)Element);
            jQueryObject ie = jQuery.FromHtml(type);
            if (ie.Is("input,textarea,select,button"))
                ie.AddClass("Input").Data("fieldName", fieldName).Attribute("tabindex", (++_tabIndex).ToString());
            Truple tu = (Truple)
                   new Truple()
                       .FromHtml("<div class='Truple'/>")
                       .AddClass(key)
                       .AppendTo(appendTo)
                       .Append(jQuery.FromHtml("<span class='Label'/>"))
                       .Append(ie)
                ;
            Truples[fieldName] = tu;
            Elements[fieldName] = tu.Element;
            return tu;
        }

        private Truple BuildTrupleBtn(string type, string fieldName, Truple appendTo)
        {
            appendTo = Script.Value(appendTo, (Truple)(object)Element);
            return (Truple)
                   new Truple()
                       .FromHtml("<div class='Truple'/>")
                       .AddClass(fieldName.Substr(0, 1).ToUpperCase() + fieldName.Substr(1))
                       .AppendTo(appendTo)
                       .Append(jQuery.FromHtml(type).AddClass("Control").Data("fieldName", fieldName))
                ;
        }

        // ReSharper disable CSharpWarnings::CS0108
        #pragma warning disable 108,114
        public Truple FromHtml(string html)
        #pragma warning restore 108,114
        // ReSharper restore CSharpWarnings::CS0108
        {
            base.FromHtml(html);
            Element.Data("truple", this);
            return this;
        }

        [AlternateSignature]
        public extern Truple Input(string fieldName);

        [AlternateSignature]
        public extern Truple Input(string fieldName, jQueryObject tu);

        public Truple Input(string fieldName, Truple tu)
        {
            return (Truple)BuildTruple("<input type='text'/>", fieldName, tu).AddClass("Text");
        }

        [AlternateSignature]
        public extern Truple Checkbox(string fieldName);

        [AlternateSignature]
        public extern Truple Checkbox(string fieldName, jQueryObject tu);

        public Truple Checkbox(string fieldName, Truple tu)
        {
            return (Truple)BuildTruple("<input type='checkbox'/>", fieldName, tu).AddClass("Checkbox");
        }

        [AlternateSignature]
        public extern Truple SelectBox(string fieldName);

        [AlternateSignature]
        public extern Truple SelectBox(string fieldName, jQueryObject tu);

        public Truple SelectBox(string fieldName, Truple tu)
        {
            return (Truple)BuildTruple("<select/>", fieldName, tu).AddClass("Select");
        }

        [AlternateSignature]
        public extern Truple Textarea(string fieldName);

        [AlternateSignature]
        public extern Truple Textarea(string fieldName, jQueryObject tu);

        public Truple Textarea(string fieldName, Truple tu)
        {
            return (Truple)BuildTruple("<textarea/>", fieldName, tu).AddClass("Textarea");
        }

        [AlternateSignature]
        public extern Truple Button(string fieldName);

        [AlternateSignature]
        public extern Truple Button(string fieldName, jQueryObject tu);

        public Truple Button(string fieldName, Truple tu)
        {
            return (Truple)BuildTrupleBtn("<button/>", fieldName, tu).AddClass("Button");
        }

        public Truple MultipleChoice(string fieldName, Dictionary question)
        {
            Truple mc = (Truple)BuildTruple("<dl/>", fieldName).AddClass("MultipleChoice");
            jQueryObject dl = jQuery.Select("dl", mc.Element);

            string qq = question["question"].ToString();
            if (question.ContainsKey("alias"))
                qq = String.Format("Hi {0},<br>{1}", question["alias"], qq);

            jQuery.FromHtml("<dt/>")
                .AppendTo(dl)
                .Html(qq)
                ;

            foreach (KeyValuePair<string, string> p in (Dictionary)question["answers"])
            {
                jQueryObject answer = jQuery.FromHtml("<dd/>")
                    .AppendTo(dl)
                    .Html(p.Value)
                    ;

                jQuery.FromHtml("<input type='radio' class='Radio'/>")
                    .PrependTo(answer)
                    .Attribute("name", fieldName)
                    .Attribute("value", p.Key)
                    ;
            }

            if ((bool)question["hide"])
                mc.Element.Hide();
            return mc;
        }


        public Truple SubHtml(string s, string html)
        {
            Select(s).Html(html);
            return this;
        }

        public Truple SubValue(string s, string value)
        {
            jQueryObject el = Select(s);
            if (el.Is("input, textarea, select"))
                el.Value(value);
            else
                el.Html(value);
            return this;
        }

        public jQueryObject InputElement()
        {
            return Select(".Input");
        }

        public Truple Value(string value)
        {
            return SubValue(".Input", value);
        }

        public Truple SubAttr(string s, string attr, string value)
        {
            Select(s).Attribute(attr, value);
            return this;
        }

        public Truple SubOptions(string s, Dictionary items)
        {
            jQueryObject el = Select(s);

            foreach (KeyValuePair<string, string> p in items)
            {
                el.Append(jQuery.FromHtml("<option/>").Attribute("value", p.Key).Html(p.Value));
            }

            return this;
        }
    }
}